package cn.minhx.domain.activity.service.trial.node;

import cn.minhx.domain.activity.model.entity.MarketProductEntity;
import cn.minhx.domain.activity.model.entity.TrialBalanceEntity;
import cn.minhx.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import cn.minhx.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import cn.minhx.types.design.tree.StrategyHandler;
import cn.minhx.types.enums.ResponseCode;
import cn.minhx.types.exception.AppException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author minhx
 */
@Slf4j
@Service
public class SwitchNode extends AbstractGroupBuyMarketSupport {

	@Resource
	private MarketNode marketNode;

	@Override
	protected TrialBalanceEntity doApply(MarketProductEntity requestParameter,
			DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
		// 根据用户ID切量
		String userId = requestParameter.getUserId();
		// 判断是否降级
		if (repository.downgradeSwitch()) {
			log.info("拼团活动降级拦截 {}", userId);
			throw new AppException(ResponseCode.E0003);
		}
		// 切量范围判断
		if (!repository.cutRange(userId)) {
			log.info("拼团活动切量拦截 {}", userId);
			throw new AppException(ResponseCode.E0004);
		}
		return router(requestParameter, dynamicContext);
	}

	@Override
	public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> next(
			MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext)
			throws Exception {
		return marketNode;
	}
}
